package com.njzxw.fs.commonutil.imp;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.njzxw.fs.commonutil.bean.MsmBean;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@Service
public class AliyunMsmImp extends MsmUtilAbs{

    /**
     * MsmBean msmBean = new MsmBean();
     //                msmBean.setTemplate_code(common.getSystem("DX.GUAMAI.TEMP"));
     //        msmBean.setTemplateParam("code",dxCode+"");
     //        msmBean.setSign_name(common.getSystem("DX.CODE.SIGN.NAME"));
     //        msmBean.setOper_id(userInfoBean.getOper_id());
     //        msmBean.setMobile(userInfoBean.getPhone());
     //        msmBean.setType(2);
     //                msmInterface.sendMsm(msmBean);
     //                logger.debug("短信是否发送成功："+msmBean.getState()+" 发送内容："+msmBean.toString());
     * @param msmBean
     */
    @Override
    public void sendMsm(@Validated MsmBean msmBean) {
        String msg = null;
        if(StringUtils.isBlank(msmBean.getSign_name())){
            msg = "短信签名sign_name不可为空";
        }
        if(StringUtils.isBlank(msmBean.getMobile())){
            msg = "手机号mobile不可为空";
        }
        if(StringUtils.isBlank(msmBean.getTemplate_code())){
            msg = "短信模板template_code不可为空";
        }

        msmBean.setTemplate_param(JSONObject.fromObject(msmBean.getTemplateParamMap()).toString());

        try {
            if(StringUtils.isBlank(msg)) {
                //可自助调整超时时间
                System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
                System.setProperty("sun.net.client.defaultReadTimeout", "10000");

                logger.debug("code:",common.getSystem("ACCESSKEYID")+"-===-"+ common.getSystem("ACCESSKEYSECRET"));
                //初始化acsClient,暂不支持region化
                IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", common.getSystem("ACCESSKEYID"), common.getSystem("ACCESSKEYSECRET"));

                DefaultProfile.addEndpoint("cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
                IAcsClient acsClient = new DefaultAcsClient(profile);

                //组装请求对象-具体描述见控制台-文档部分内容
                SendSmsRequest request = new SendSmsRequest();
                //必填:待发送手机号xxxx
                request.setPhoneNumbers(msmBean.getMobile());
                //必填:短信签名-可在短信控制台中找到
                request.setSignName(msmBean.getSign_name());
                //必填:短信模板-可在短信控制台中找到

                request.setTemplateCode(msmBean.getTemplate_code());

                if(!StringUtils.isBlank(msmBean.getTemplate_param())){
                    //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                    request.setTemplateParam(msmBean.getTemplate_param());
                }

                if(!StringUtils.isBlank(msmBean.getExtend_code())){
                    //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
                    request.setSmsUpExtendCode(msmBean.getExtend_code());
                }
                if(!StringUtils.isBlank(msmBean.getOut_id())) {
                    //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                    request.setOutId(msmBean.getOut_id());
                }

                SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

                msmBean.setCode(sendSmsResponse.getCode());
                msmBean.setBiz_id(sendSmsResponse.getBizId());
                msmBean.setMessage(sendSmsResponse.getMessage());
                msmBean.setRequest_id(sendSmsResponse.getRequestId());
                msmBean.setState("OK".equals(sendSmsResponse.getCode())?1:0);
            //记录到数据库
            }else{
                msmBean.setCode("NO");
                msmBean.setMessage(msg);
                msmBean.setState(0);
            }
        } catch (Exception e) {
            logger.error("短信发送失败:"+msmBean.toString(),e);
            msmBean.setCode("NO");
            msmBean.setMessage("短信发送异常报错："+e.getMessage());
            msmBean.setState(0);
        } finally {
            baseRep.insertMsm(msmBean);
        }

    }



}
